package com.oracle.zebrafreelytraveling.ui;

import android.app.Activity;
import android.app.Service;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;

import com.amap.api.navi.AMapNavi;
import com.amap.api.navi.AMapNaviListener;
import com.amap.api.navi.AMapNaviView;
import com.amap.api.navi.AMapNaviViewListener;
import com.amap.api.navi.enums.NaviType;
import com.amap.api.navi.model.AMapLaneInfo;
import com.amap.api.navi.model.AMapNaviCameraInfo;
import com.amap.api.navi.model.AMapNaviCross;
import com.amap.api.navi.model.AMapNaviInfo;
import com.amap.api.navi.model.AMapNaviLocation;
import com.amap.api.navi.model.AMapNaviTrafficFacilityInfo;
import com.amap.api.navi.model.AMapServiceAreaInfo;
import com.amap.api.navi.model.AimLessModeCongestionInfo;
import com.amap.api.navi.model.AimLessModeStat;
import com.amap.api.navi.model.NaviInfo;
import com.amap.api.navi.model.NaviLatLng;
import com.amap.api.navi.view.RouteOverLay;
import com.amap.api.services.core.LatLonPoint;
import com.autonavi.tbt.TrafficFacilityInfo;
import com.oracle.zebrafreelytraveling.App;
import com.oracle.zebrafreelytraveling.R;
import com.oracle.zebrafreelytraveling.data.model.NaviInfoModel;
import com.oracle.zebrafreelytraveling.map.TTSController;
import com.oracle.zebrafreelytraveling.utils.ToastUtil;
import com.oracle.zebrafreelytraveling.utils.UserUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by yydcnjjw on 2017/8/1.
 */

public class NaviActivity extends Activity implements AMapNaviViewListener, AMapNaviListener {

    public static final String FROM = "from";
    public static final String TO = "to";
    public static final String POSITION = "position";

    private AMapNaviView mAMapNaviView;
    private AMapNavi mAMapNavi;
    private RouteOverLay mRouteOverlay;
    int strategyFlag = 0;
    // 起点坐标集合
    private List<NaviLatLng> startList = new ArrayList<>();

    // 途径点坐标集合

    private List<NaviLatLng> wayList = new ArrayList<>();

    // 终点坐标集合［建议就一个终点］
    private List<NaviLatLng> endList = new ArrayList<>();

    private NaviLatLng startLaLng;
    private NaviLatLng endLaLng;
    private int position;
    //讯飞语言合成
    TTSController mTtsManager;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_navi);
        getIntentData();
        initTTS();
        mAMapNaviView = (AMapNaviView) findViewById(R.id.map_navi);
        mAMapNaviView.onCreate(savedInstanceState);
        initAMapNaviView();
        initMapNavi();
        initPiont();
    }

    private void initAMapNaviView(){
        mAMapNaviView.setAMapNaviViewListener(this);
        mAMapNaviView.setNaviMode(AMapNaviView.CAR_UP_MODE);
    }

    private void getIntentData() {
        Intent intent = getIntent();
        if (intent != null) {
            LatLonPoint from = intent.getParcelableExtra(FROM);
            LatLonPoint to = intent.getParcelableExtra(TO);
            position = intent.getIntExtra(POSITION, -1);
            startLaLng = new NaviLatLng(from.getLatitude(), from.getLongitude());
            endLaLng = new NaviLatLng(to.getLatitude(), to.getLongitude());
        }
    }

    /**
     * 初始化坐标信息
     */
    private void initPiont() {
        startList.add(startLaLng);
        endList.add(endLaLng);
    }

    /**
     * 初始化路径规划
     */
    private void initMapNavi() {
        mAMapNavi = AMapNavi.getInstance(getApplicationContext());
        mAMapNavi.addAMapNaviListener(mTtsManager);
        mAMapNavi.addAMapNaviListener(this);
        mAMapNavi.setEmulatorNaviSpeed(60);

    }

    private void initTTS() {
        mTtsManager = TTSController.getInstance(App.getContextObject());
        mTtsManager.init();
    }

    private void calculateDriveRoute() {
        try {
            strategyFlag = mAMapNavi.strategyConvert(true, false, false, true, false);
        } catch (Exception e) {
            e.printStackTrace();
        }
        mAMapNavi.calculateDriveRoute(startList, endList, wayList, strategyFlag);
    }

    public static void startNaviActivity(Activity activity,
                                         LatLonPoint from, LatLonPoint to, int position) {
        Intent intent = new Intent(activity, NaviActivity.class);
        intent.putExtra(FROM, from);
        intent.putExtra(TO, to);
        intent.putExtra(POSITION, position);
        activity.startActivity(intent);
    }

    public static void startNaviActivity(Service service,
                                         LatLonPoint from, LatLonPoint to, int position) {
        Intent intent = new Intent(service, NaviActivity.class);
        intent.putExtra(FROM, from);
        intent.putExtra(TO, to);
        intent.putExtra(POSITION, position);
        service.startActivity(intent);
    }

    @Override
    protected void onResume() {
        super.onResume();
        mAMapNaviView.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        mAMapNaviView.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mAMapNaviView.onDestroy();
        mAMapNavi.removeAMapNaviListener(mTtsManager);
        mAMapNavi.stopNavi();
    }

    /*********************************AMapNaviViewListener***********************/


    @Override
    public void onNaviSetting() {

    }

    @Override
    public void onNaviCancel() {
        finish();
    }

    @Override
    public boolean onNaviBackClick() {
        return false;
    }

    @Override
    public void onNaviMapMode(int i) {

    }

    @Override
    public void onNaviTurnClick() {

    }

    @Override
    public void onNextRoadClick() {

    }

    @Override
    public void onScanViewButtonClick() {

    }

    @Override
    public void onLockMap(boolean b) {

    }

    @Override
    public void onNaviViewLoaded() {

    }


    /*************************************AMapNaviListener*****************************************/
    @Override
    public void onInitNaviFailure() {
        Log.d("onInitNaviFailure", "onInitNaviFailure");
    }

    @Override
    public void onInitNaviSuccess() {
        calculateDriveRoute();
    }

    @Override
    public void onStartNavi(int i) {
        Log.d("onStartNavi", i+"");
    }

    @Override
    public void onTrafficStatusUpdate() {

    }

    @Override
    public void onLocationChange(AMapNaviLocation aMapNaviLocation) {
        Log.d("onLocationChange", aMapNaviLocation.getCoord().getLatitude()+"LOCATION");
    }

    @Override
    public void onGetNavigationText(int i, String s) {
        Log.d("onGetNavigationText", s+ " " +i);
        ToastUtil.showToast("导航"+s);
    }

    @Override
    public void onEndEmulatorNavi() {

    }

    @Override
    public void onArriveDestination() {
        if (position != -1){
            NaviInfoModel model = UserUtil.getUserNaviModelList();
            List<NaviInfoModel.bean> list = model.getNaviInfoModelList();
            NaviInfoModel.bean bean = list.get(position);
            bean.setNavied(true);
            list.set(position, bean);
        }
    }

    @Override
    public void onCalculateRouteSuccess() {
        mAMapNavi.startNavi(NaviType.GPS);
    }

    @Override
    public void onCalculateRouteFailure(int i) {
        Log.d("onCalculateRouteFailure", i+"");
    }

    @Override
    public void onReCalculateRouteForYaw() {

    }

    @Override
    public void onReCalculateRouteForTrafficJam() {

    }

    @Override
    public void onArrivedWayPoint(int i) {

    }

    @Override
    public void onGpsOpenStatus(boolean b) {

    }

    @Override
    public void onNaviInfoUpdate(NaviInfo naviInfo) {

    }

    @Override
    public void onNaviInfoUpdated(AMapNaviInfo aMapNaviInfo) {

    }

    @Override
    public void updateCameraInfo(AMapNaviCameraInfo[] aMapNaviCameraInfos) {

    }

    @Override
    public void onServiceAreaUpdate(AMapServiceAreaInfo[] aMapServiceAreaInfos) {

    }

    @Override
    public void showCross(AMapNaviCross aMapNaviCross) {

    }

    @Override
    public void hideCross() {

    }

    @Override
    public void showLaneInfo(AMapLaneInfo[] aMapLaneInfos, byte[] bytes, byte[] bytes1) {

    }

    @Override
    public void hideLaneInfo() {

    }

    @Override
    public void onCalculateMultipleRoutesSuccess(int[] ints) {

    }

    @Override
    public void notifyParallelRoad(int i) {

    }

    @Override
    public void OnUpdateTrafficFacility(AMapNaviTrafficFacilityInfo aMapNaviTrafficFacilityInfo) {

    }

    @Override
    public void OnUpdateTrafficFacility(AMapNaviTrafficFacilityInfo[] aMapNaviTrafficFacilityInfos) {

    }

    @Override
    public void OnUpdateTrafficFacility(TrafficFacilityInfo trafficFacilityInfo) {

    }

    @Override
    public void updateAimlessModeStatistics(AimLessModeStat aimLessModeStat) {

    }

    @Override
    public void updateAimlessModeCongestionInfo(AimLessModeCongestionInfo aimLessModeCongestionInfo) {

    }

    @Override
    public void onPlayRing(int i) {

    }
}
